var eventproxy = require('eventproxy')
var superagent = require('superagent')
var cheerio = require('cheerio')
var async = require('async')

var url = require('url')

var cnodeUrl = 'http://ireadweek.com/index.php/Index/index.html'

superagent.get(cnodeUrl)
    .end(function(err, res) {
        if (err) {
            return console.error(err)
        }

        var topicUrls = []
            // load txt
        var $ = cheerio.load(res.text)
            // 
        $('.hanghang-list>a').each(function(idx, element) {
            var $element = $(element);

            var href = url.resolve(cnodeUrl, $element.attr('href'))
            topicUrls.push(href)
        })

        var ep = new eventproxy()

        ep.after('topic_html', topicUrls.length, function(topics) {
            topics = topics.map(function(topicPair) {
                var topicUrl = topicPair[0]
                var topicHtml = topicPair[1]
                var $ = cheerio.load(topicHtml)
                return ({
                    title: $('.hanghang-shu-content-btns').closest('a').attr('href').trim(),
                    href: topicUrl,
                    comment1: $('.hanghang-za-title').text().trim()
                })
            })

            console.log('final:')
            console.log(topics)
        })
        topicUrls.forEach(function(topicUrl) {
            var delay = parseInt(Math.random() * 1000) //100-500ms
            setTimeout(function() {
                superagent.get(topicUrl)
                    .end(function(err, res) {
                        console.log('fetch ' + topicUrl + ' successful')
                        ep.emit('topic_html', [topicUrl, res.text])
                    })
            }, delay)
        })


        // console.log(topicUrls)
    })